﻿using EFCore.Models;
using Microsoft.EntityFrameworkCore;
using SIService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SService
{
    public class Sys_SubordinateS: Sys_SubordinateSI
    {
        private readonly WmssystemContext myDbContext;

        public Sys_SubordinateS(WmssystemContext myDbContext)
        {
            this.myDbContext = myDbContext;
        }
        //添加
        public Task<int> Add(Sys_Subordinate subAdd)
        {
            myDbContext.Sys_Subordinate.Add(subAdd);
            return myDbContext.SaveChangesAsync();
        }
        //模糊查询
        public async Task<List<Sys_Subordinate>> FenYe(string type, string Label = null, bool? Status = null)
        {
            var query = myDbContext.Sys_Subordinate.AsQueryable();
            // 根据条件进行查询
            if (!string.IsNullOrEmpty(Label))
            {
                query = query.Where(c => c.D_DDictionaryLabel.Contains(Label));
            }
            if (Status.HasValue)
            {
                query = query.Where(c => c.D_DStatus == Status);
            }
            //逻辑删除条件筛选
            query = query.Where(c => c.D_DIsDelete == true);
            // 使用 Where() 获取符合条件的实体集合
            var result = await query.Where(x => x.D_DType == type).ToListAsync();

            // 返回结果集合
            return result;

            //query = query.FirstOrDefaultAsync(x => x.D_DType == type);
            //// 查询
            //return query.ToListAsync();

            // return query.Skip((currentPage - 1) * pageSize).Take(pageSize).ToListAsync();
        }
        //修改
        public Task<int> Upd(Sys_Subordinate subUpd)
        {
            myDbContext.Sys_Subordinate.Update(subUpd);
            return myDbContext.SaveChangesAsync();
        }

        public async Task<int> UpdIsDelete(int id, bool isDeleted)
        {
            var entity = await myDbContext.Sys_Subordinate.FindAsync(id);
            // 更新实体的删除状态
            entity.D_DIsDelete = isDeleted;
            // 标记实体为已修改状态
            myDbContext.Entry(entity).State = EntityState.Modified;
            // 保存更改到数据库
            return await myDbContext.SaveChangesAsync();
        }
    }
}
